package net.dgg.liuyinglong.plugin.forms;

import com.intellij.openapi.ui.Messages;

import javax.swing.*;
import java.awt.event.*;

public class PluginDialogUseCase extends JDialog {
    private JPanel contentPane;
    private JButton buttonOK;
    private JButton buttonCancel;
    private JTextField et_usecase_name;
    private JComboBox cb_response_type;
    private JTextField et_response_val;
    private JTextField et_jk_name;
    private JComboBox cb_net_type;
    private JTextField et_net_params;
    private DialogCallBack dialogCallBack;

    public PluginDialogUseCase(DialogCallBack dialogCallBack) {
        this.dialogCallBack = dialogCallBack;
        setContentPane(contentPane);
        setModal(true);
        getRootPane().setDefaultButton(buttonOK);

        buttonOK.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onOK();
            }
        });

        buttonCancel.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        });

        // call onCancel() when cross is clicked
        setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                onCancel();
            }
        });

        // call onCancel() on ESCAPE
        contentPane.registerKeyboardAction(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                onCancel();
            }
        }, KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
    }

    private void onOK() {
        String name = et_usecase_name.getText().trim();
        if ("".equals(name)) {
            Messages.showInfoMessage(this, "UseCaseName不能为空", "提示");
            return;
        }
        String jkName = et_jk_name.getText().trim();
        if ("".equals(jkName)) {
            Messages.showInfoMessage(this, "接口名称不能为空", "提示");
            return;
        }
        String netType = cb_net_type.getSelectedItem().toString();
        String netParams = et_net_params.getText().trim();
        // add your code here
        if (null != dialogCallBack) {
            dialogCallBack.ok(name, getTypeString(), jkName, netType, netParams);
        }
        dispose();
    }

    /**
     * 获取类型名称
     *
     * @return
     */
    private String getTypeString() {
        String rets = et_response_val.getText().trim();
        int pos = cb_response_type.getSelectedIndex();
        switch (pos) {
            case 0:
                rets = "List<" + rets + ">";
                break;
            case 1:
                break;
            case 2:
                rets = "JSONObject";
                break;
        }
        return rets;
    }

    private void onCancel() {
        // add your code here if necessary
        dispose();
    }

    public static void main(String[] args) {
        PluginDialogUseCase dialog = new PluginDialogUseCase((name, jsonType, jkName, netType, netParams) -> {
            System.out.print("name>>" + name + "   jsonType>>" + jsonType + "   jkName>>" + jkName + "   netType>>" + netType + "   netParams>>" + netParams);
        });
        dialog.pack();
        dialog.setVisible(true);
        System.exit(0);
    }

    public interface DialogCallBack {
        void ok(String name, String jsonType, String jkName, String netType, String netParams);
    }
}
